package com.sadhoc.router.duplicate;

import java.util.HashMap;

import com.sadhoc.router.topology.SequenceNumber;
import com.sadhoc.router.util.ExpiringHashMap;

public class DuplicateTable extends ExpiringHashMap<DuplicateTableEntry, Boolean> {

	private static final long serialVersionUID = -1930065738372970799L;

	/*
	 * This method checks to see if the table containts the entry
	 */
	public boolean contains(String originator, SequenceNumber sequenceNumber) {
		return this.containsKey(new DuplicateTableEntry(originator, sequenceNumber));
	}
	
	/*
	 * Formats the table as a string
	 */
	public String toString() {
		String s = "Originator           \tSequence Number\n";
		s += "-----------\t--------\n";
	
		synchronized(getLock()) {
			for (DuplicateTableEntry key : keySet()) {
				s += key.getOriginator() + "\t" + key.getSequenceNumber().getSeqNum() + "\n";
			}
		}
		return s;
	}
	
	/*
	 * This method returns an exact copy of the table
	 */
	public DuplicateTable clone() {
		
		DuplicateTable table = new DuplicateTable(); 
		synchronized(getLock()) {
			HashMap<DuplicateTableEntry, Boolean> x = getCopyOfAllData();
			for (DuplicateTableEntry key : keySet()) {
				
				Boolean e = x.get(key);
				table.addEntry(new DuplicateTableEntry(key.getOriginator(), key.getSequenceNumber()), e, this.get(key));
			}
		}
		
		return table;
		
	}
	

	public void onRemoved(DuplicateTableEntry key) {
		// We're not doing anything with this
	}
	
}
